<template>
  <el-dialog
    :title="!dataForm.configId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="参数名称" prop="configName">
      <el-input v-model="dataForm.configName" placeholder="参数名称"></el-input>
    </el-form-item>
    <el-form-item label="参数键名" prop="configKey">
      <el-input v-model="dataForm.configKey" placeholder="参数键名"></el-input>
    </el-form-item>
    <el-form-item label="参数键值" prop="configValue">
      <el-input v-model="dataForm.configValue" placeholder="参数键值"></el-input>
    </el-form-item>
    <el-form-item label="系统内置（Y是 N否）" prop="configType">
      <el-input v-model="dataForm.configType" placeholder="系统内置（Y是 N否）"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新者"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          configId: 0,
          configName: '',
          configKey: '',
          configValue: '',
          configType: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: '',
          remark: ''
        },
        dataRule: {
          configName: [
            { required: true, message: '参数名称不能为空', trigger: 'blur' }
          ],
          configKey: [
            { required: true, message: '参数键名不能为空', trigger: 'blur' }
          ],
          configValue: [
            { required: true, message: '参数键值不能为空', trigger: 'blur' }
          ],
          configType: [
            { required: true, message: '系统内置（Y是 N否）不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.configId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.configId) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/sysconfig/info/${this.dataForm.configId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.configName = data.sysConfig.configName
                this.dataForm.configKey = data.sysConfig.configKey
                this.dataForm.configValue = data.sysConfig.configValue
                this.dataForm.configType = data.sysConfig.configType
                this.dataForm.createBy = data.sysConfig.createBy
                this.dataForm.createTime = data.sysConfig.createTime
                this.dataForm.updateBy = data.sysConfig.updateBy
                this.dataForm.updateTime = data.sysConfig.updateTime
                this.dataForm.remark = data.sysConfig.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/sysconfig/${!this.dataForm.configId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'configId': this.dataForm.configId || undefined,
                'configName': this.dataForm.configName,
                'configKey': this.dataForm.configKey,
                'configValue': this.dataForm.configValue,
                'configType': this.dataForm.configType,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'updateBy': this.dataForm.updateBy,
                'updateTime': this.dataForm.updateTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
